package com.asianrapid.controller.sys;

import com.asianrapid.model.sys.PageResult;
import com.asianrapid.model.sys.StaffEntity;
import com.asianrapid.service.sys.StaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class StaffController {

    private Logger log = LoggerFactory.getLogger(StaffController.class);

    @Resource(name = "staffServiceImpl")
    private StaffService staffService;

    @GetMapping("/staff/{name}")
    public StaffEntity staffGet(@PathVariable String name) {
        StaffEntity staffEntity = staffService.getStaffEntityByName(name);
        log.debug("The method is ending");
        return staffEntity;
    }

    /**
     * 获取staff表数据
     *
     * @param name
     * @param pageSize
     * @param page
     * @return
     */
    @GetMapping("/staffs")
    public PageResult staffsList(String name, int pageSize, int page) {
        PageResult pageResult = new PageResult();
        pageResult.setData(staffService.staffsList(name, pageSize, page * pageSize));
        pageResult.setTotalCount(staffService.staffsSize(name, pageSize, page * pageSize));
        log.debug("The method is ending");
        return pageResult;
    }

    /**
     * 新建用户信息
     *
     * @param staffEntity
     * @return
     */
    @PostMapping("/staffs/staff")
    public StaffEntity insertStaff(@RequestBody StaffEntity staffEntity) {

        staffService.insertStaff(staffEntity);
        log.debug("The method is ending");
        return staffEntity;
    }

    /**
     * 更新用户信息
     *
     * @param staffEntity
     * @param id
     * @return
     */
    @PutMapping("/staffs/{id}")
    public StaffEntity updateStaff(@RequestBody StaffEntity staffEntity, @PathVariable int id) {
        if (staffEntity.getId() == id) {
            staffService.updateStaff(staffEntity);
        }
        return staffEntity;
    }

    /**
     * 删除用户信息
     *
     * @param groupId
     * @return
     */
    @DeleteMapping("/staffs/delete")
    public List<String> deleteStaffs(@RequestBody List<String> groupId) {
        staffService.deleteStaffs(groupId);
        log.debug("The method is ending");
        return groupId;
    }


}
